<?php
 /**
* @property Email $Email
  * 
  */
class SendMailComponent extends Component{
    var $components = array('Email', 'Sms');
    private $controller;
    function initialize(&$controller, $settings = array()) {
        $this->controller =$controller;
    }
    public function send_mail($type,$to,$subject,$values=array(), $replies = array()){
         
        $option = ClassRegistry::init('Setting');
        $template = ClassRegistry::init('Message');

        App::uses('CakeEmail', 'Network/Email');                       
        $send = new CakeEmail();
        $send->to("$to");
        $send->subject("$subject");
        $send->from(array('info@loyalty.com' => 'Loyalty Virgin'));
        $send->template();
        $send->emailFormat('html');
        $send->replyTo = $option->get_option('EMAIL_PARSING') ;
  
        if(isset($values['ATTACHMENT']) && !empty($values['ATTACHMENT'])){
                //$attachedFilePath = WWW_ROOT . 'img' . DS . 'ticket' . DS . 'attachment' . DS .$values['ATTACHMENT'];
               // $attachedFile = $values['ATTACHMENT'];
                $send->attachments($values['ATTACHMENT']);
        }

        $content = $template->get_template($type);
        $content = strip_tags($content,'<br>');
        $content = str_replace("\n", "<br/>", $content);
        $view = new View();
        $absolute_path = Router::url('/', true);
        $content =  $view->element("email_template", array('content'=>$content,'js_absolute_path'=>$absolute_path));
        
        if(!empty($values)){
            foreach($values as $key => $value){
                $content = str_replace("{".$key."}", $value, $content);
            }
        }
        try {
            $send->send($content);
        } catch (Exception $e) {
            //trigger_error("Network ERROR: couldn't send email");
            return false;
        }
        return true;
    }
    public function survey_added($to,$values) {
        
        $option = ClassRegistry::init('Setting');
        $template = ClassRegistry::init('Message');
        App::uses('CakeEmail', 'Network/Email');    
        $send = new CakeEmail();
        $send->to("$to");
        $send->subject("A new survey added");
        $send->from(array('info@emstraining.com' => 'Loyalty Virgin'));
        $send->template();
        $send->emailFormat('html');
        $content = $template->getTemplateById(Message::NEW_SURVEY_ADDED,"vn");
        $content = strip_tags($content,'<br>');
        $content = str_replace("\n", "<br/>", $content);
        $view = new View();
        $absolute_path = Router::url('/', true);
        $content =  $view->element("email_template", array('content'=>$content,'js_absolute_path'=>$absolute_path));
        //$content = $this->controller->View->element("email_template", array('content'=>$content));
        
        if(!empty($values)){
            foreach($values as $key => $value){
                $content = str_replace("{".$key."}", $value, $content);
            }
        }
        try {
            $send->send($content);
        } catch (Exception $e) {
            //trigger_error("Network ERROR: couldn't send email");
            //$this->controller->Session->setFlash("Network ERROR: couldn't send email");
            //$this->controller->redirect(array('action'=>'index','controller'=>'vouchers'));
            return false;
        }
        return true;
    }
    public function send_voucher($template_id,$language,$to,$values=array()){
        
        $option = ClassRegistry::init('Setting');
        $template = ClassRegistry::init('Message');

        App::uses('CakeEmail', 'Network/Email');                       
        $send = new CakeEmail();
        $send->to("$to");
        $send->subject("You got a voucher");
        $send->from(array('info@emstraining.com' => 'Loyalty Virgin'));
        $send->template();
        $send->emailFormat('html');
        
        if(isset($values['ATTACHMENT']) && !empty($values['ATTACHMENT'])){
            $send->attachments($values['ATTACHMENT']);
        }

        $content = $template->getTemplateById($template_id,$language);
        $content = strip_tags($content,'<br>');
        $content = str_replace("\n", "<br/>", $content);
        $view = new View();
        $absolute_path = Router::url('/', true);
        $content =  $view->element("email_template", array('content'=>$content,'js_absolute_path'=>$absolute_path));
        //$content = $this->controller->View->element("email_template", array('content'=>$content));
        
        if(!empty($values)){
            foreach($values as $key => $value){
                $content = str_replace("{".$key."}", $value, $content);
            }
        }
        try {
            $send->send($content);
            if(isset($values['ATTACHMENT']) && !empty($values['ATTACHMENT'])){
                unlink($values['ATTACHMENT']);
                $png = str_replace(".pdf", ".png", $values['ATTACHMENT']);
                unlink($png);
            }
        } catch (Exception $e) {
            //trigger_error("Network ERROR: couldn't send email");
            //$this->controller->Session->setFlash("Network ERROR: couldn't send email");
            //$this->controller->redirect(array('action'=>'index','controller'=>'vouchers'));
            return false;
        }
        return true;
    }
    public function send($type,$to,$subject,$values=array(), $replies = array()){
         
        $option = ClassRegistry::init('Setting');
        $template = ClassRegistry::init('Message');

        App::uses('CakeEmail', 'Network/Email');                       
        $send = new CakeEmail();
        $send->to("$to");
        $send->subject("$subject");
        $send->from(array('info@loyalty.com' => 'Loyalty Virgin'));
        $send->template();
        $send->emailFormat('html');
        $content = $template->get_template($type);
        if(!empty($values)){
            foreach($values as $key => $value){
                $content = str_replace("{".$key."}", $value, $content);
            }
        }
        if(isset($values['ATTACHMENT']) && !empty($values['ATTACHMENT'])){
            $send->attachments($values['ATTACHMENT']);
        }
        try {
            $send->send($content);
        } catch (Exception $e) {
            //trigger_error("Network ERROR: couldn't send email");
            return false;
        }
        return true;
    }

    public function send_message($to, $subject, $content){

        App::uses('CakeEmail', 'Network/Email');
        $send = new CakeEmail();
        $send->to($to);
        $send->subject("$subject");
        $send->from(array('info@emstraining.com' => 'Loyalty Virgin'));
        $send->template();
        $send->emailFormat('html');
       // $send->replyTo = $option->get_option('EMAIL_PARSING') ;
        //$content= str_replace("\n", "<br/>", $content);
        //$content = $this->controller->View->element("email_template", array('content'=>$content));
        $send->send($content);
        return true;
    }


    public function notifications($type, $to, $subject, $values=array()){
        
        $option = ClassRegistry::init('Setting');
        $template = ClassRegistry::init('Message');

        $priority = isset($values['PRIORITY'])? $values['PRIORITY']: '';
        $mobile = isset($values['MOBILE'])? $values['MOBILE']: '';

        if($priority == 'SMS' && !empty($mobile)){
        
             $content = $template->get_template($type);
        
             if(!empty($values)){
                  foreach($values as $key => $value){
                      $content = str_replace("{".$key."}", $value, $content);
                  }
             }
        
             $message = strip_tags($content);
             $from = "Virgin Loyalty Program";
             $response = $this->Sms->text_message( $mobile, $from, $message );

        }else{

             $this->send_mail($type, $to, $subject, $values);
        
        }
        return true;
    }


}